﻿using Deployment.Data.Deploy.Steps.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Deployment.Data.UI.Controls.Task
{
    /// <summary>
    /// Interaction logic for TaskEditorControl.xaml
    /// </summary>
    public partial class TaskEditorControl : UserControl
    {
        public static readonly DependencyProperty EnvironmentsProperty =
            DependencyProperty.Register("Environments",
                typeof(Data.Environment.EnvironmentsFile),
                typeof(TaskEditorControl));

        public static readonly DependencyProperty CurrentStepProperty =
            DependencyProperty.Register("CurrentStep",
                typeof(Data.Deploy.Steps.Step),
                typeof(TaskEditorControl));

        public static readonly DependencyProperty ContentOkPropery =
            DependencyProperty.Register("isContentOk",
                typeof(bool),
                typeof(TaskEditorControl));

        public Data.Environment.EnvironmentsFile Environments
        {
            get { return (Data.Environment.EnvironmentsFile)GetValue(EnvironmentsProperty); }
            set 
            { 
                SetValueDp(EnvironmentsProperty, value);
                (this.DataContext as AbstractTask).TaskControl.EnvironmentsFile = this.Environments;
            }
        }

        public Data.Deploy.Steps.Step CurrentStep
        {
            get { return (Data.Deploy.Steps.Step)GetValue(CurrentStepProperty); }
            set 
            { 
                SetValueDp(CurrentStepProperty, value);
                updateTaskId();
            }
        }

        public bool isContentOk
        {
            get { return (bool)GetValue(ContentOkPropery); }
            set { SetValueDp(ContentOkPropery, value); }
        }
        public event DependencyPropertyChangedEventHandler PropertyChanged;

        private void SetValueDp(DependencyProperty property, object value)
        {
            SetValue(property, value);
            if (PropertyChanged != null)
                PropertyChanged(this, new DependencyPropertyChangedEventArgs());
        }

        public TaskEditorControl()
        {
            InitializeComponent();
        }

        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext != null && this.DataContext is AbstractTask)
            {
                (this.DataContext as AbstractTask).TaskControl.EnvironmentsFile = this.Environments;
                updateTaskId();
            }
        }

        private void updateTaskId()
        {
            if (CurrentStep != null && this.DataContext != null && this.DataContext is AbstractTask && (this.DataContext as AbstractTask).Id == -1)
            {
                int maxTaskId = 0;
                foreach (AbstractTask task in CurrentStep.Tasks)
                {
                    if (task.Id > maxTaskId)
                        maxTaskId = task.Id;
                }
                (this.DataContext as AbstractTask).Id = (maxTaskId + 1);
            }
        }

        private void placeholderCheckbox_Checked(object sender, RoutedEventArgs e)
        {
            isContentOk = placeholderCheckbox.IsChecked == true ? true : false;
        }
    }
}
